package com.hzj.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzj.pojo.Person;
import com.hzj.mapper.PersonMapper;
import com.hzj.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hzj
 * @since 2021-03-10
 */
@Service
public class PersonServiceImpl implements PersonService {
    @Autowired
    PersonMapper pm;

    @Override
    public int add(Person person) {

        return pm.insert(person);
    }

    @Override
    public int delete(int id) {
        return pm.deleteById(id);
    }

    @Override
    public int update(Person person) {
        return pm.updateById(person);
    }

    @Override
    public List<Person> showAll() {
        return pm.selectList(null);
    }

    @Override
    public Person showPerson(int id) {
        return pm.selectById(id);
    }

    @Override
    public Page selectPage(int pageIndex, int pageSize) {
//        System.out.println(pageIndex + "|" + pageSize);
        Page page1 = new Page(pageIndex, pageSize);

        Page page2 = pm.selectPage(page1, null);
        if (!page2.hasPrevious()) {
            page2.setCurrent(1);
        }
        if (!page2.hasNext()) {
//            System.out.println(page2.getPages());
            page2.setCurrent(page2.getPages());
        }
        return page2;
    }
}
